package com.Vronsky.config;

import com.Vronsky.core.dataSource.SimpleDataSource;
import com.Vronsky.core.exception.VronskyCommonException;
import com.Vronsky.core.utils.Assert;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

/**
 * 数据源配置信息，待完善
 */
public class DataSourceProperties {

    private String url;
    private String jdbcDriver;

    private  String host;
    private  int port;
    private  String user;
    private String password;
    private String dataBase;

    private Integer initializationConnNum;
    private Integer maxConnNum;

    private static DataSourceProperties dataSourceProperties;
    private long timeOut;

    static {
        initDataSourceProperties();
    }

    /**
     * @description 如果他来调这个方法，类首先会被加载，类被加载dataSourceProperties肯定不为空，直接返回即可
     * @return com.Vronsky.config.DataSourceProperties
     * @author zjh
     * @date 2024/7/11 10:15
     */
    public static DataSourceProperties getDataSourceProperties(){
        return dataSourceProperties;
    }

    /**
     * @description 初始化数据源配置
     *      <ul>
     *          <li> 显示调用 Class.forName 加载</li>
     *          <li> 或者在new的时候 加载</li>
     *          <li> 类只会被加载一次，所以不会有线程安全问题</li>
     *      </ul>
     * @param
     * @return void
     * @author zjh
     * @date 2024/7/11 9:51
     */
    private static void initDataSourceProperties(){
        //如果dataSourceProperties是空的，就new一个
        dataSourceProperties = Optional.ofNullable(dataSourceProperties).orElseGet(DataSourceProperties::new);
        Properties props = new Properties();
        try (InputStream input = SimpleDataSource.class.getClassLoader().getResourceAsStream("simpleDataSource.properties")) {
            if (input == null) {
                throw new FileNotFoundException("Property file 'simpleDataSource.properties' not found in the classpath");
            }
            //读取
            props.load(input);
            //设置值，在set方法里校验
            dataSourceProperties.setUrl(props.getProperty("url"));
            dataSourceProperties.setJdbcDriver(props.getProperty("jdbcDriver"));
            dataSourceProperties.setHost(props.getProperty("host"));
            dataSourceProperties.setPort(props.getProperty("port"));
            dataSourceProperties.setUser(props.getProperty("user"));
            dataSourceProperties.setPassword(props.getProperty("password"));
            dataSourceProperties.setDataBase(props.getProperty("dataBase"));
            dataSourceProperties.setInitializationConnNum(props.getProperty("initializationConnNum"));
            dataSourceProperties.setMaxConnNum(props.getProperty("maxConnNum"));
            dataSourceProperties.setTimeOut(Long.valueOf(props.getProperty("timeOut")));
        } catch (IOException e) {
            throw new VronskyCommonException(e.getMessage());
        }
    }

    public String getUrl() {
        return url;
    }

    public long getTimeOut() {
        return timeOut;
    }

    public void setTimeOut(long timeOut) {
        this.timeOut = timeOut;
    }

    public void setUrl(String url) {
        Assert.hasText(url,"url不能为空!");
        this.url = url;
    }

    public String getJdbcDriver() {
        return jdbcDriver;
    }

    public void setJdbcDriver(String jdbcDriver) {
        Assert.hasText(jdbcDriver,"jdbcDriver不能为空!");
        this.jdbcDriver = jdbcDriver;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        Assert.hasText(host,"host不能为空!");
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(String port) {
        Assert.hasText(port,"port不能为空!");
        this.port = Integer.parseInt(port);
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        Assert.hasText(user,"user不能为空!");
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        Assert.hasText(password,"password不能为空!");
        this.password = password;
    }

    public String getDataBase() {
        return dataBase;
    }

    public void setDataBase(String dataBase) {
        Assert.hasText(dataBase,"dataBase不能为空!");
        this.dataBase = dataBase;
    }

    public Integer getInitializationConnNum() {
        return initializationConnNum;
    }

    public void setInitializationConnNum(String initializationConnNum) {
        //其实这个可以不写，如果为空的话给个默认值就行了
        Assert.hasText(initializationConnNum,"initializationConnNum不能为空!");
        this.initializationConnNum = Integer.parseInt(initializationConnNum);
    }

    public Integer getMaxConnNum() {
        return maxConnNum;
    }

    public void setMaxConnNum(String maxConnNum) {
        //其实这个可以不写，如果为空的话给个默认值就行了
        Assert.hasText(maxConnNum,"maxConnNum不能为空!");
        this.maxConnNum = Integer.parseInt(maxConnNum);
    }


}
